#include "esp_log.h"
#include "esp_event.h"
#include "esp_lvgl_port.h"
#include "wired_debug_ui.h"
#include "main_ui.h"
#include "md_images.h"
#include "md_languages.h"
#include "ui_setting.h"
#include "func_pin_image.h"
#include "work_status_setting.h"
#include "esp_jtag_main.h"
#include "usbd_core.h"
#include "dap_main.h"
#include "dap_usb_handle.h"
#include "dap_setting.h"

static const char *TAG = "wifi_setting_ui";
// 声明字体
LV_FONT_DECLARE(myFont);
LV_FONT_DECLARE(lv_font_montserrat_14);

#define MAIN_SCREEN         0
#define DAP_LINK_SCREEN     1
#define ESP_UF2_JTAG_SCREEN 2
static uint8_t current_screen = 0;

static lv_obj_t *current_wired_debug_screen = NULL;
static lv_obj_t *wired_debug_screen_old = NULL;
static lv_obj_t *esp_uf2_jtag_page = NULL;
static lv_obj_t *esp_dap_link_page = NULL;

static void dap_link_event_cb(lv_event_t *e);     // 扫描按钮事件回调
static void esp_uf2_jtag_event_cb(lv_event_t *e); // 配网按钮事件回调
static void refresh_wired_debug_ui();
void exit_wired_debug_ui(lv_obj_t *root)
{
    //lvgl_port_lock(0); // 进入LVGL临界区（确保线程安全）

    if (MAIN_SCREEN == current_screen) {
        switch_to_main_screen(); // 切换到主界面
        /*删除对应的逻辑分析仪界面，防止多次调用，内存泄漏*/
        set_work_status(USUALLY_WORK);
        /*因为功能界面多,而且不常用,所以删除*/
        lv_obj_del(root); //也可以把lv_scr_load_anim的auto_del设置为true
    } else if (DAP_LINK_SCREEN == current_screen) {
        refresh_wired_debug_ui();
        if (esp_dap_link_page) {
            lv_obj_del(esp_dap_link_page); //也可以把lv_scr_load_anim的auto_del设置为true
            esp_dap_link_page = NULL;
        }
        /*删除dap-link*/
        dap_usb_deinit(0);

        current_screen = MAIN_SCREEN;
    } else if (ESP_UF2_JTAG_SCREEN == current_screen) {
        refresh_wired_debug_ui();
        if (esp_uf2_jtag_page) {
            lv_obj_del(esp_uf2_jtag_page); //也可以把lv_scr_load_anim的auto_del设置为true
            esp_uf2_jtag_page = NULL;
        }
        /*删除esp-uf2-jtag*/
        esp_jtag_deinit(0);

        current_screen = MAIN_SCREEN;
    }
   // lvgl_port_unlock(); // 退出LVGL临界区
}

// 手势事件回调
static void event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_GESTURE) {
        lv_indev_t *indev = lv_indev_get_act();
        if (!indev)
            return;

        lv_dir_t dir = lv_indev_get_gesture_dir(indev);

        // 保留右划检测
        if (dir == LV_DIR_RIGHT || dir == LV_DIR_TOP) {
            exit_wired_debug_ui(e->target); // 使用 current_target 而不是 target
        }
    }
}

// 刷新设置界面函数
static void refresh_wired_debug_ui()
{
    wired_debug_screen_old = current_wired_debug_screen;

    // 创建新界面
    lv_obj_t *new_screen = create_wired_debug_ui();
    if (new_screen) {
        // 加载新界面
        lv_scr_load(new_screen);
        // 删除旧界面
        lv_obj_del(wired_debug_screen_old);
        wired_debug_screen_old = NULL;
    }
}

/* 
 * @ brief:创建无线调试UI
*/
lv_obj_t *create_wired_debug_ui(void)
{
    lvgl_port_lock(0); // 进入LVGL临界区（确保线程安全）
    // 创建根容器（全屏黑色背景）
    lv_obj_t *root = lv_obj_create(NULL);
    if (!root)
        return NULL;

    lv_obj_remove_style_all(root); // 移除所有默认样式
    lv_obj_set_size(root, LV_PCT(100), LV_PCT(100));
    uint32_t color = get_screen_color_val();
    lv_obj_set_style_bg_color(root, lv_color_hex(color), 0);
    lv_obj_set_style_bg_opa(root, LV_OPA_COVER, 0);
    lv_obj_set_style_pad_all(root, 10, 0); // 添加内边距
    lv_obj_add_event_cb(root, event_handler, LV_EVENT_GESTURE, NULL);

    // 创建功能列表
    lv_obj_t *list = lv_list_create(root);
    lv_obj_set_size(list, LV_PCT(100), LV_PCT(100));
    lv_obj_set_style_bg_color(list, lv_color_hex(color), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(list, LV_OPA_70, LV_PART_MAIN);
    lv_obj_set_style_border_width(list, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(list, 10, LV_PART_MAIN);
    lv_obj_set_style_text_font(list, &myFont, LV_PART_MAIN);
    lv_obj_set_style_text_color(list, get_btn_font_color(), LV_PART_MAIN);
    lv_obj_set_style_pad_all(list, 10, LV_PART_MAIN); // 列表内边距

    // 添加功能按钮 - 使用更明显的样式
    lv_obj_t *btn;

    // Wi-Fi扫描按钮
    btn = lv_list_add_btn(list, NULL, "DAP-Link");
    lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_radius(btn, 8, LV_PART_MAIN);
    lv_obj_set_style_pad_all(btn, 15, LV_PART_MAIN); // 按钮内边距
    lv_obj_add_event_cb(btn, dap_link_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_bg_color(btn, lv_color_hex(color), LV_PART_MAIN);
    lv_obj_set_style_text_color(btn, get_btn_font_color(), LV_PART_MAIN);
    // SmartConfig按钮
    btn = lv_list_add_btn(list, NULL, "esp-uf2-jtag");
    lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_radius(btn, 8, LV_PART_MAIN);
    lv_obj_set_style_pad_all(btn, 15, LV_PART_MAIN);
    lv_obj_add_event_cb(btn, esp_uf2_jtag_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_bg_color(btn, lv_color_hex(color), LV_PART_MAIN);
    lv_obj_set_style_text_color(btn, get_btn_font_color(), LV_PART_MAIN);

    //保存
    current_wired_debug_screen = root;
    current_screen = MAIN_SCREEN; // 切换到主界面
    lvgl_port_unlock();           // 退出LVGL临界区
    return root;
}
/**********************************************dap-link****************************************************************/
/**
 * @brief 下拉列表事件回调函数
 * @param e LVGL事件对象
 */
static void dropdown_event_cb(lv_event_t *e)
{
    lv_obj_t *dropdown = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_VALUE_CHANGED) {
        // 获取当前选中的选项索引
        uint16_t selected_idx = lv_dropdown_get_selected(dropdown);

        // 获取选项文本
        char buf[32];
        lv_dropdown_get_selected_str(dropdown, buf, sizeof(buf));
        MdLink_DAP_Setting_t Dap_Setting = DAP_SETTING_DEFAULT();
        // 根据选择的模式执行相应操作
        switch (selected_idx) {
            case 0: // IO模拟
                ESP_LOGI(TAG, "DAP-Link mode switched to: IO");

                Dap_Setting.swd_port_mode = PORT_MODE_GPIO;
                set_dap_setting(&Dap_Setting, false);
                // 这里可以添加IO模拟模式的初始化代码
                // set_dap_mode(DAP_MODE_IO_EMU);
                break;

            case 1: // 专用IO模拟
                ESP_LOGI(TAG, "DAP-Link mode switched to: DEDIC_IO");
                // 这里可以添加专用IO模拟模式的初始化代码
                // set_dap_mode(DAP_MODE_DEDICATED_IO);
                Dap_Setting.swd_port_mode = PORT_MODE_DEDIC_GPIO;
                set_dap_setting(&Dap_Setting, false);
                break;

            case 2: // SPIMN
                ESP_LOGI(TAG, "DAP-Link mode switched to: GP_SPI");
                // 这里可以添加SPIMN模式的初始化代码
                // set_dap_mode(DAP_MODE_SPIMN);
                Dap_Setting.swd_port_mode = PORT_MODE_SPI;
                set_dap_setting(&Dap_Setting, false);
                break;

            default:
                ESP_LOGE(TAG, "Unknown DAP-Link mode selected: %d", selected_idx);
                break;
        }

    }
}
/*
* @brief: dap-link 按钮事件回调
*/
static void dap_link_event_cb(lv_event_t *e)
{
    set_work_status(DAP_WORK);
    current_screen = DAP_LINK_SCREEN;


    lvgl_port_lock(0); // 进入LVGL临界区（确保线程安全）
    // 创建根容器（全屏黑色背景）
    esp_dap_link_page = lv_obj_create(NULL);

    lv_obj_set_size(esp_dap_link_page, lv_pct(100), lv_pct(100));
    lv_obj_set_style_bg_color(esp_dap_link_page, lv_color_black(), LV_PART_MAIN);
    lv_obj_add_event_cb(esp_dap_link_page, event_handler, LV_EVENT_GESTURE, NULL);

    // 创建主布局容器（垂直排列）
    lv_obj_t *main_container = lv_obj_create(esp_dap_link_page);
    lv_obj_remove_style_all(main_container);
    lv_obj_set_size(main_container, lv_pct(100), lv_pct(100));
    lv_obj_set_flex_flow(main_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_pad_all(main_container, 5, LV_PART_MAIN); //

    // // 创建主布局容器（垂直排列）
    // esp_dap_link_page = lv_obj_create(lv_scr_act());
    // lv_obj_remove_style_all(esp_dap_link_page);
    // lv_obj_set_size(esp_dap_link_page, lv_pct(100), lv_pct(100));
    // lv_obj_center(esp_dap_link_page);
    // lv_obj_set_style_bg_color(esp_dap_link_page, lv_color_black(), LV_PART_MAIN);
    // lv_obj_set_style_bg_opa(esp_dap_link_page, LV_OPA_COVER, LV_PART_MAIN);
    // lv_obj_set_style_border_width(esp_dap_link_page, 0, 0);
    // lv_obj_set_style_radius(esp_dap_link_page, 10, 0);
    // lv_obj_set_flex_flow(esp_dap_link_page, LV_FLEX_FLOW_COLUMN);
    // lv_obj_set_style_pad_all(esp_dap_link_page, 20, 0); // 设置内边距

    // 添加手势事件处理
    //lv_obj_add_event_cb(esp_dap_link_page, event_handler, LV_EVENT_GESTURE, NULL);

    /******************** 顶部区域 ********************/
    lv_obj_t *top_container = lv_obj_create(main_container);
    lv_obj_remove_style_all(top_container);
    lv_obj_set_size(top_container, lv_pct(100), lv_pct(45));
    lv_obj_set_flex_flow(top_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(top_container,
                          LV_FLEX_ALIGN_SPACE_EVENLY,
                          LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER);

    // 左侧徽标容器
    lv_obj_t *logo_container = lv_obj_create(top_container);
    lv_obj_remove_style_all(logo_container);
    lv_obj_set_size(logo_container, LV_SIZE_CONTENT, lv_pct(90));
    lv_obj_set_flex_align(logo_container,
                          LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER);

    const void *img_src = get_func_image_data(FUNC_WIRED_DEBUG, WIRED_DEBUG_DAP_NUM);
    if (img_src) {
        lv_obj_t *logo_img = lv_img_create(logo_container);
        if (logo_img) {
            lv_img_set_src(logo_img, img_src);
            lv_obj_center(logo_img);
        }
    }

    // 右侧信息面板
    lv_obj_t *info_panel = lv_obj_create(top_container);
    lv_obj_remove_style_all(info_panel);
    lv_obj_set_size(info_panel, lv_pct(60), lv_pct(90));
    lv_obj_set_style_bg_color(info_panel, lv_color_hex(0x212121), LV_PART_MAIN);
    lv_obj_set_style_radius(info_panel, 12, LV_PART_MAIN);
    lv_obj_set_style_pad_all(info_panel, 15, LV_PART_MAIN);
    lv_obj_set_flex_flow(info_panel, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(info_panel,
                          LV_FLEX_ALIGN_SPACE_BETWEEN,
                          LV_FLEX_ALIGN_START,
                          LV_FLEX_ALIGN_CENTER);

    /******************** 设备信息 ********************/
    lv_obj_t *info_label = lv_label_create(info_panel);
    if (info_label) {
        lv_obj_set_style_text_font(info_label, &lv_font_montserrat_14, LV_PART_MAIN);
        lv_label_set_text(info_label, "Device: MdLink\n"
                                      "cdc-acm :virtual com port\n"
                                      "swd/jtag:debug arm chips");
        lv_obj_set_style_text_color(info_label, lv_color_white(), LV_PART_MAIN);
        lv_obj_set_style_text_line_space(info_label, 8, LV_PART_MAIN);
        lv_obj_set_style_pad_top(info_label, 5, LV_PART_MAIN); // 添加底部内边距
    }
    /******************** 底部区域 ********************/
    lv_obj_t *bottom_container = lv_obj_create(main_container);
    lv_obj_remove_style_all(bottom_container);
    lv_obj_set_size(bottom_container, lv_pct(100), lv_pct(55));
    lv_obj_set_flex_flow(bottom_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_scrollbar_mode(bottom_container, LV_SCROLLBAR_MODE_OFF); // 禁止滚动条
    lv_obj_set_scroll_dir(bottom_container, LV_DIR_NONE);               // 完全禁用滚动
    lv_obj_clear_flag(bottom_container, LV_OBJ_FLAG_SCROLLABLE);        // 禁止默认滚动
    lv_obj_add_flag(bottom_container, LV_OBJ_FLAG_EVENT_BUBBLE);        // 允许事件冒泡

    /******************** 状态栏 ********************/
    lv_obj_t *status_bar = lv_obj_create(bottom_container);
    lv_obj_remove_style_all(status_bar);
    lv_obj_set_size(status_bar, lv_pct(100), 40); // 增加高度以适应更大字体
    lv_obj_set_flex_flow(status_bar, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(status_bar,
                          LV_FLEX_ALIGN_SPACE_BETWEEN, // 修改为两端对齐
                          LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_bottom(status_bar, 10, LV_PART_MAIN);

    // 标题 - 向左对齐
    lv_obj_t *title = lv_label_create(status_bar);
    if (title) {
        lv_label_set_text(title, "DAP-Link");
        lv_obj_set_style_text_color(title, lv_color_hex(0x6200EE), LV_PART_MAIN);
        lv_obj_set_style_text_font(title, &myFont, LV_PART_MAIN);
        lv_obj_set_style_text_align(title, LV_TEXT_ALIGN_LEFT, LV_PART_MAIN); // 左对齐
    }

    // 右侧下拉列表
    lv_obj_t *dropdown = lv_dropdown_create(status_bar);
    if (dropdown) {
        lv_dropdown_set_options(dropdown, "IO模拟\n专用IO模拟\nSPI模拟");
        lv_obj_set_style_text_font(dropdown, &lv_font_montserrat_14, LV_PART_MAIN);
        lv_obj_set_style_bg_color(dropdown, lv_color_hex(0x333333), LV_PART_MAIN);
        lv_obj_set_style_text_font(dropdown, &myFont, LV_PART_MAIN);
        lv_obj_set_style_text_color(dropdown, lv_color_white(), LV_PART_MAIN);
        lv_obj_set_style_border_color(dropdown, lv_color_hex(0x6200EE), LV_PART_MAIN);
        lv_obj_set_style_border_width(dropdown, 1, LV_PART_MAIN);
        lv_obj_set_style_radius(dropdown, 5, LV_PART_MAIN);
        lv_obj_set_size(dropdown, 180, 40);

        // 添加下拉列表事件回调（如果需要）
       lv_obj_add_event_cb(dropdown, dropdown_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    }

    /******************** 引脚状态显示区域 ********************/

    char *pin_names[16] = { "tx", "NC", "rx", "rst", "NC", "nRST", "dio/tms", "clk/tck", "tdi", "tdo", "NC", "NC", "NC", "3v3", "5v0", "GND" };
    bool pin_status[16] = { true, false, true, true, false, true, true, true, true, true, false, false, false, true, true, true };
    uint32_t pin_color[16] = { 0x00FF00, 0x00D2FF,  0x00FF00, 0x00FF00, 0x00D2FF, 0x00FF00, 0x00FF00, 0x00FF00, 0x00FF00, 0x00FF00, 0x00D2FF, 0x00D2FF, 0x00D2FF, 0xFF0000, 0xFF0000, 0x666666 };
    pin_item_t pins;
    pins.name = pin_names;
    pins.status = pin_status;
    pins.color = pin_color;

    func_pin_image(bottom_container, 2, 8, &pins);
   
    // //删除主界面
    // if (current_wired_debug_screen) {
    //     lv_obj_del(current_wired_debug_screen);
    // }
    lv_scr_load_anim(esp_dap_link_page, LV_SCR_LOAD_ANIM_NONE, 0, 0, false);
    lvgl_port_unlock(); // 退出LVGL临界区

    /*启动dap-link*/
    dap_usb_init(0, ESP_USBD_BASE);
}
/**********************************************esp-uf2-jtag****************************************************************/

// esp_uf2_jtag UI
static void esp_uf2_jtag_ui_create(void)
{

    // 创建主布局容器（垂直排列）
   // esp_uf2_jtag_page = lv_obj_create(lv_scr_act());

    // lv_obj_remove_style_all(esp_uf2_jtag_page);
    // lv_obj_set_size(esp_uf2_jtag_page, lv_pct(100), lv_pct(100));
    // lv_obj_set_flex_flow(esp_uf2_jtag_page, LV_FLEX_FLOW_COLUMN);
    // lv_obj_set_style_pad_all(esp_uf2_jtag_page, 5, LV_PART_MAIN); //
    // lv_obj_set_style_bg_color(esp_uf2_jtag_page, lv_color_black(), LV_PART_MAIN);
    // lv_obj_set_style_bg_opa(esp_uf2_jtag_page, LV_OPA_COVER, LV_PART_MAIN);

    lvgl_port_lock(0);                                         // 进入LVGL临界区（确保线程安全）
    // 创建根容器（全屏黑色背景）
    esp_uf2_jtag_page = lv_obj_create(NULL);

    lv_obj_set_size(esp_uf2_jtag_page, lv_pct(100), lv_pct(100));
    lv_obj_set_style_bg_color(esp_uf2_jtag_page, lv_color_black(), LV_PART_MAIN);
    lv_obj_add_event_cb(esp_uf2_jtag_page, event_handler, LV_EVENT_GESTURE, NULL);

    // 创建主布局容器（垂直排列）
    lv_obj_t *main_container = lv_obj_create(esp_uf2_jtag_page);
    lv_obj_remove_style_all(main_container);
    lv_obj_set_size(main_container, lv_pct(100), lv_pct(100));
    lv_obj_set_flex_flow(main_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_pad_all(main_container, 5, LV_PART_MAIN); //
    // 添加手势事件处理
    //  lv_obj_add_event_cb(esp_uf2_jtag_page, event_handler, LV_EVENT_GESTURE, NULL);

    /******************** 顶部区域 ********************/
    lv_obj_t *top_container = lv_obj_create(main_container);

    lv_obj_remove_style_all(top_container);
    lv_obj_set_size(top_container, lv_pct(100), lv_pct(45));
    lv_obj_set_flex_flow(top_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(top_container,
                          LV_FLEX_ALIGN_SPACE_EVENLY,
                          LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER);

    // 左侧徽标容器
    lv_obj_t *logo_container = lv_obj_create(top_container);
    lv_obj_remove_style_all(logo_container);
    lv_obj_set_size(logo_container, LV_SIZE_CONTENT, lv_pct(90));
    lv_obj_set_flex_align(logo_container,
                          LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER);

    const void *img_src = get_func_image_data(FUNC_WIRED_DEBUG, WIRED_DEBUG_ESP_NUM);
    if (img_src) {
        lv_obj_t *logo_img = lv_img_create(logo_container);
        if (logo_img) {
            lv_img_set_src(logo_img, img_src);
            lv_obj_center(logo_img);
        }
    }

    // 右侧信息面板
    lv_obj_t *info_panel = lv_obj_create(top_container);
    lv_obj_remove_style_all(info_panel);
    lv_obj_set_size(info_panel, lv_pct(60), lv_pct(90));
    lv_obj_set_style_bg_color(info_panel, lv_color_hex(0x212121), LV_PART_MAIN);
    lv_obj_set_style_radius(info_panel, 12, LV_PART_MAIN);
    lv_obj_set_style_pad_all(info_panel, 15, LV_PART_MAIN);
    lv_obj_set_flex_flow(info_panel, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(info_panel,
                          LV_FLEX_ALIGN_SPACE_BETWEEN,
                          LV_FLEX_ALIGN_START,
                          LV_FLEX_ALIGN_CENTER);

    /******************** 设备信息 ********************/
    lv_obj_t *info_label = lv_label_create(info_panel);
    if (info_label) {
        lv_obj_set_style_text_font(info_label, &lv_font_montserrat_14, LV_PART_MAIN);
        lv_label_set_text(info_label, "Device: MdLink\n"
                                      "uf2:Virtual USB flash drive\n"
                                      "jtag:Debug all ESP chips");
        lv_obj_set_style_text_align(info_label, LV_TEXT_ALIGN_LEFT, 0);
        lv_obj_set_style_text_color(info_label, lv_color_white(), LV_PART_MAIN);
        lv_obj_set_style_text_line_space(info_label, 8, LV_PART_MAIN);
        lv_obj_set_style_pad_top(info_label, 5, LV_PART_MAIN); // 添加底部内边距
    }

    /******************** 底部区域 ********************/
    lv_obj_t *bottom_container = lv_obj_create(main_container);
    lv_obj_remove_style_all(bottom_container);
    lv_obj_set_size(bottom_container, lv_pct(100), lv_pct(55));
    lv_obj_set_flex_flow(bottom_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_scrollbar_mode(bottom_container, LV_SCROLLBAR_MODE_OFF); // 禁止滚动条
    lv_obj_set_scroll_dir(bottom_container, LV_DIR_NONE);               // 完全禁用滚动
    lv_obj_clear_flag(bottom_container, LV_OBJ_FLAG_SCROLLABLE);        // 禁止默认滚动
    lv_obj_add_flag(bottom_container, LV_OBJ_FLAG_EVENT_BUBBLE);        // 允许事件冒泡

    /******************** 状态栏 ********************/
    lv_obj_t *status_bar = lv_obj_create(bottom_container);
    lv_obj_remove_style_all(status_bar);
    lv_obj_set_size(status_bar, lv_pct(100), 40); // 增加高度以适应更大字体
    lv_obj_set_flex_flow(status_bar, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(status_bar,
                          LV_FLEX_ALIGN_CENTER, // 居中对齐
                          LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_bottom(status_bar, 10, LV_PART_MAIN);

    // 标题 - 使用更大更显眼的字体
    lv_obj_t *title = lv_label_create(status_bar);
    if (title) {
        lv_label_set_text(title, "ESP UF2-JTAG");
        lv_obj_set_style_text_color(title, lv_color_hex(0x6200EE), LV_PART_MAIN);
        lv_obj_set_style_text_font(title, &myFont, LV_PART_MAIN); // 使用更大的字体
        lv_obj_set_style_text_align(title, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
    }

    /******************** 引脚状态显示区域 ********************/
    char *pin_names[16] = { "boot", "tx", "rx", "rst", "NC", "nRST", "tms", "tck", "tdi", "tdo", "NC", "NC", "NC", "3v3", "5v0", "GND" };
    bool pin_status[16] = { true, true, true, true, false, true, true, true, true, true, false, false, false, true, true, true };
    uint32_t pin_color[16] = { 0x00FF00, 0x00FF00, 0x00FF00, 0x00FF00, 0x00D2FF, 0x00FF00, 0x00FF00, 0x00FF00, 0x00FF00, 0x00FF00, 0x00D2FF, 0x00D2FF, 0x00D2FF, 0xFF0000, 0xFF0000, 0x666666 };
    pin_item_t pins;
    pins.name = pin_names;
    pins.status = pin_status;
    pins.color = pin_color;

    func_pin_image(bottom_container, 2, 8, &pins);
    //更新状态
    current_screen = ESP_UF2_JTAG_SCREEN;

    lv_scr_load_anim(esp_uf2_jtag_page, LV_SCR_LOAD_ANIM_NONE, 0, 0, false);
    lvgl_port_unlock(); // 退出LVGL临界区

    set_work_status(ESP_JTAG_WORK);
    /*  启动esp-uf2-jtag*/
    esp_jtag_init(0, ESP_USBD_BASE);


    //     //删除主界面
    // if (current_wired_debug_screen) {
    //     lv_obj_del(current_wired_debug_screen);
    // }
}

//  按钮事件回调
static void esp_uf2_jtag_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED) {
        esp_uf2_jtag_ui_create();
        // 确保新界面可见
    }
}